import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {environment} from '@env/environment';

@Injectable({
  providedIn: 'root'
})

export class SystemConfigService {

  constructor(private http: HttpClient) { }

  getSysInfo() {
    return new Promise((resolve, reject) => {
      this.http.get(`hmconfig/api/sysInfo`).subscribe(res => {
          resolve(res);
        }, error => {
          reject(error);
        }
      );
    });
  }

   getMenusTree(id?: string) {
     return new Promise((resolve, reject) => {
       this.http.get(`${environment.CONFIGURL}api/menus/tree`, {params: {roleId: id}}).subscribe(res => {
         resolve(res);
       }, error => {
         reject(error);
       });
     });
   }

   // 员工类型列表信息
  getStaffPage(p) {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/staffs/page`, {params: p}).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 删除员工
  delStaff(id) {
    return new Promise((resolve, reject) => {
      this.http.delete(`${environment.CONFIGURL}api/staffs/${id}`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 当前员工相似信息查询
  queryStaffInfo(id) {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/staffs/${id}`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 查询员工类型
  getStaffsTypes(data) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.CONFIGURL}api/selectCommonDic/multi`, data).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }
  // 部门管理列表
  depTree() {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/departments/tree`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 新增、修改部门
  depTreeMod(data) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.CONFIGURL}api/departments`, data).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 部门详情
  depTreeDetail(id) {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/departments/${id}`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 部门删除
  depTreeDel(id) {
    return new Promise((resolve, reject) => {
      this.http.delete(`${environment.CONFIGURL}api/departments/${id}`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 部门表格信息
  depTreeTable(params) {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/departments/page`, {params}).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 字典表
  selCom(params) {
    return new Promise((resolve, reject) => {

      this.http.get(`${environment.CONFIGURL}api/selectCommonDic`, {params: {tableName: params}}).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 修改menu的字段
  updateMenuInfo(params) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.CONFIGURL}api/menus`, params).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 系统信息模块
  getSys() {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/sysInfo`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  getSysType() {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/selectCommonDic?orderBy=sort&tableName=tb_dic_sys_type`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 系统信息修改
  sysMsgUpdate(data) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.CONFIGURL}api/sysInfo`, data).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 角色管理列表
  getRoleInfo(params) {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/roles/page`, {params: params}).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 角色管理新增修改
  roleInfoUpdate(data) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.CONFIGURL}api/roles`, data).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 角色管理详情
  roleInfoById(id) {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/roles/${id}`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 删除角色管理
  roleInfoDel(id) {
    return new Promise((resolve, reject) => {
      this.http.delete(`${environment.CONFIGURL}api/roles/${id}`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 角色菜单配置
  menuConf(data) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.CONFIGURL}api/roles`, data).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 用户管理模块
  // 用户管理列表
  getUserInfo(params) {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/users/page`, {params: params}).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 角色下拉框
  getRole() {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/roles/page?pageNum=1&pageSize=9999`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 用户管理详情
  userInfoById(id) {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/users/${id}`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 用户管理修改
  userInfoUpdate(data) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.CONFIGURL}api/users`, data).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 删除用户管理
  userInfoDel(id) {
    return new Promise((resolve, reject) => {
      this.http.delete(`${environment.CONFIGURL}api/users/${id}`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 用户管理密码修改
  userInfoPass(data) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.CONFIGURL}api/users/password/change/other`, data).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 个人中心模块
  getpersonInfo() {
    return new Promise((resolve, reject) => {
      this.http.get(`${environment.CONFIGURL}api/users/current`).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 修改当前登陆用户的默认角色
  updateCurrentDefault(id) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.CONFIGURL}api/users/current/roles/default/${id}`, {}).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  // 删除menu的节点
  deleteNode(id) {
    return new Promise((resolve, reject) => {
      this.http.delete(`${environment.CONFIGURL}api/menus/` + id).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  putPassowrd(data) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.CONFIGURL}api/users/password/change`, data).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }
}
